import { Component, Node } from "cc";
import { UIView } from "./UIView";
import { SoundUrl } from "../data/GameConfig";

export class UIComponent extends Component {
    private _uiView: UIView;
    public get uiView() {
        if (!this._uiView) {
            this._uiView = this.findUIView(this.node);
        }
        return this._uiView;
    }
    public get uiName() {
        return this.uiView?.uiName || '';
    }
    private findUIView(node: Node) {
        if (!node) {
            return null;
        }
        this._uiView = node.getComponent(UIView);
        if (!this._uiView) {
            return this.findUIView(node.parent);
        }
        return this._uiView;
    }
    public bindButton(node: Node, listener: Function, target = null, args?:any[], sound : string|null = SoundUrl.ui) {
        this.uiView?.bindButton(node, listener, target, args, sound);
    }
    public bindClick(node: Node, listener: Function, target = null, args?:any[], sound : string|null = SoundUrl.ui) {
        this.uiView?.bindClick(node, listener, target, args, sound);
    }
}